// vue.config.js 是脚手架的配置文件; 修改了必须重启项目
// 使用js的严格模式， 注意：严格模式下 this如果指向undefined，说明指向window
'use strict'
// 导入nodejs的path模块
const path = require('path')
// 导入src下面的settings.js的配置
const defaultSettings = require('./src/settings.js')
//  将相对路径转为绝对路径
function resolve(dir) {
  return path.join(__dirname, dir)
}
// 定义网页的标题
const name = defaultSettings.title || 'vue Admin Template' // page title

// 设置项目运行的端口(读取环境变量里面的prot，如果没有读取packagejson里面的port，如果还没有使用9528)
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// 定义配置  https://cli.vuejs.org/config/
module.exports = {
  // 资源引用路径
  publicPath: '/',
  // 打包输出目录
  outputDir: 'dist',
  // 打包静态文件目录
  assetsDir: 'static',
  // 是否开启代码校验
  lintOnSave: process.env.NODE_ENV === 'development',
  // 是否关闭map文件
  productionSourceMap: false,
  // 开发服务器介绍
  devServer: {
    port: port, // 端口号
    open: false, // 运行好了之后是否自动打开浏览器
    overlay: {
      warnings: false,
      errors: true
    },
    // 代理跨域的配置
    proxy: {
      // 当我们的本地的请求 有/api的时候，就会代理我们的请求地址向另外一个服务器发出请求
      '/api': {
        target: 'http://ihrm.itheima.net/', // 跨域请求的地址
        changeOrigin: true // 只有这个值为true的情况下 才表示开启跨域
      }
    }
    // 注释掉这一句！
    // before: require("./mock/mock-server.js"),
  },
  // webpack的配置【对象式配置】
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name, // 网页标题
    resolve: {
      // 路径解析
      alias: {
        '@': resolve('src') // 如果遇到@src进行解析为根目录的scr目录
      }
    }
  },
  // webpack的配置【函数式配置】
  chainWebpack(config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV !== 'development', (config) => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [
          {
            // `runtime` must same as runtimeChunk name. default is `runtime`
            inline: /runtime\..*\.js$/
          }
        ])
        .end()
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // only package third parties that are initially dependent
          },
          elementUI: {
            name: 'chunk-elementUI', // split elementUI into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
      config.optimization.runtimeChunk('single')
    })
  }
}
